package com.jiepos.mpos.core.util;

import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.Format;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;
import java.util.UUID;

/**
 * 
* 描述: 根据时间序列生成ID编号 
* 模块: 通用平台
* @author: 娄伟峰
* version： 1.0
 */
public class IDFactoryUtil {
	 /** The FieldPosition. */
    private static final FieldPosition HELPER_POSITION = new FieldPosition(0);

    /** This Format for format the data to special format. */
    private final static Format dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

    /** This Format for format the number to special format. */
    private final static NumberFormat numberFormat = new DecimalFormat("0000");

    /** This int is the sequence number ,the default value is 0. */
    private static int seq = 0;

    private static final int MAX = 9999;
    
    
    private static int seq1 = 0;

    private static final int MAX1 = 999999;
   
    final static int min = 100000;
    final static int max = 999999;
    
    private final static Random rand = new Random(); 
  /**  
   * @return String  根据时间序列生成ID编号
   */
  public static String getId() {
	  Calendar rightNow = Calendar.getInstance();
      StringBuffer sb = new StringBuffer();
      dateFormat.format(rightNow.getTime(), sb, HELPER_POSITION);
      numberFormat.format(seq, sb, HELPER_POSITION);
      if (seq == MAX) {
          seq = 0;
      } else {
          seq++;
      }
      return sb.toString();
  }
  /**
   * 获取流水号
   * @return
   */
  public static String getTraceNo(){
	  if (seq1 == MAX1) {
          seq1 = 1;
      } else {
    	  seq1++;         
      }
	  return String.format("%06d",seq1);
  }
  
  /**
   * 获取uuid
   * @return
   */
  public static String getUUID(){
	  UUID uuid = java.util.UUID.randomUUID();
	  return uuid.toString().toUpperCase().replace("-", "");
  }
  

  /**
   * 获取6位随机数字
   * @param min
   * @param max
   * @return
   */
  public static int getSixRandom(){	  
      int tmp = Math.abs(rand.nextInt());
      return tmp % (max - min + 1) + min;
  }
 

  
  public static void main(String args[]){
	  seq1 = 1606;
	  System.out.println(getTraceNo());
  }
}